package com.homer.iot.esptouch.model;

import com.homer.iot.esptouch.tcp.UartTCPUtil;

public abstract class Slaver {
	/**
	 * 从机ID.
	 */
	private String id;
	/**
	 * 从机类型.
	 */
	private String type;

	protected Device device;
	public Slaver() {

	}

	public Slaver(String id, String type) {
		super();
		this.id = id;
		this.type = type;

	}

	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}

	public String getId() {
		return id;
	}

	public String getType() {
		return type;
	}

	/**
	 * byte数组中取int数值，本方法适用于(低位在前，高位在后)的顺序，和和intToBytes（）配套使用
	 * 
	 * @param src
	 *            byte数组
	 * @param offset
	 *            从数组的第offset位开始
	 * @return int数值
	 */
	public static int bytesToInt(byte[] src, int offset) {
		int value;
		value = (int) ((src[offset] & 0xFF) | ((src[offset + 1] & 0xFF) << 8)
				//| ((src[offset + 2] & 0xFF) << 16) | ((src[offset + 3] & 0xFF) << 24)
				);
		return value;
	}

	/**
	 * byte数组中取int数值，本方法适用于(低位在后，高位在前)的顺序。和intToBytes2（）配套使用
	 */
	public static int bytesToInt2(byte[] src, int offset) {
		int value;
		value = (int) (((src[offset] & 0xFF) << 24)
				| ((src[offset + 1] & 0xFF) << 16)
				//| ((src[offset + 2] & 0xFF) << 8) | (src[offset + 3] & 0xFF)
				);
		return value;
	}

	public double getVolt() {

		byte[] bVolt = UartTCPUtil.getVolt(this.device.getDeviceIP());
		int volt = bytesToInt(bVolt, 0);

		return volt / 0x7fff * 1.15 * 3;
	}

	public int getRSSI() {
		
		return UartTCPUtil.getRSSI(this.device.getDeviceIP());
	}

	// public int getStatus(){
	// return 0;
	// }

	public void setId(String id) {
		this.id = id;
	}

	public void setType(String type) {
		this.type = type;
	}

}
